//
//  TuijianShipinViewController.m
//  jiyujiaProgram
//
//  Created by 中良 赵 on 15/12/15.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "TuijianShipinViewController.h"
#import "TuijianShipinTableViewCell.h"
#import "VideoListViewController.h"
#import "Common.h"
#import "WeAppHttpManager.h"
#import "DictModel.h"
#import "DictData.h"
#import "VideoModel.h"

@interface TuijianShipinViewController ()
{
    NSMutableArray*sectionArr;
    NSMutableDictionary *dic;
}
@end

@implementation TuijianShipinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self creatHttpRequest];
}
- (void)creatHttpRequest
{
    NSDictionary *param = @{
                    @"JB":@"3",
                    @"PARENT_ID":@"e47ae78903c24178903557a55e02d4ef"
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(FINDDIC) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            NSLog(@"%@",result);
            [DictModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"DictData",
                         };
            }];
            sectionArr =[[NSMutableArray alloc]init];
            DictModel *dict = [[DictModel alloc] init];
            dict = [DictModel objectWithKeyValues:result];
            [sectionArr addObjectsFromArray:dict.data];
            NSLog(@"%@",sectionArr);
            if (sectionArr.count) {
                dic =[[NSMutableDictionary alloc]init];
                for (int i=0 ;i<sectionArr.count; i++) {
                    DictData *dicdata=[sectionArr objectAtIndex:i];
                
                    NSDictionary *param = @{
                                            @"CLASSIFICATION":dicdata.zd_ID,
                                            @"CATEGORY":@"",
                                            @"STICK":@"1",
                                            @"pageIndex":@"0",
                                            @"pageSize":@"2",
                                            };
                    
                    [[HttpManager defaultManager] postRequestToUrl:RequestURL(QUERYVIDEOBY) params:param complete:^(BOOL successed, NSDictionary *result) {
                        NSLog(@"result===%@",result);
                        if (successed) {
                            NSArray*arr=[[NSArray alloc]init];
                            arr=[VideoModel arrayOfModelsFromDictionaries:[result objectForKey:@"data"]];
                            NSLog(@"%@",arr);
                            if (arr.count) {
                                [dic setObject:arr forKey:dicdata.name];
                            }else{
                                [dic setObject:@[] forKey:dicdata.name];
                            }
                            NSLog(@"dic====%@",dic);
                        }
                        else{
                            [SVProgressHUD showErrorWithStatus:@"请求失败，请重试"];
                        }
                    }];
                }
                NSLog(@"dic====%@",dic);
                [_tableview reloadData];
            }
            
//            [_tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    DictData *dd =[sectionArr objectAtIndex:section];
    NSArray *arr=[dic objectForKey:dd.name];
        return arr.count/2+arr.count%2;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return sectionArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150.f;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableview.frame.size.width, 40.f)];
//    view.backgroundColor=[UIColor lightGrayColor];
    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 30)];
//    [lbl setFont:[UIFont fontWithName:SystemFont size:10]];
    [view addSubview:lbl];
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(view.frame.size.width-50, 5, 50, 30)];
    [btn setTitle:@"更多>" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont fontWithName:SystemFont size:15]];
    btn.tag=section;
    [btn addTarget:self action:@selector(moreVideoAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    DictData *dd =[sectionArr objectAtIndex:section];
    lbl.text=dd.name;
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.f;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    TuijianShipinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"TuijianShipinTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    DictData *dd =[sectionArr objectAtIndex:indexPath.section];
    NSArray *arr=[dic objectForKey:dd.name];
    if (arr.count) {
        VideoModel *vmodel=[arr objectAtIndex:0];
        cell.lblDetail1.text=vmodel.category_NAME;
        cell.lblName1.text=vmodel.video_NAME;
    cell.btn1.tag=indexPath.section*10;
    [cell.btn1 addTarget:self action:@selector(playVideoAction:) forControlEvents:UIControlEventTouchUpInside];
        if (arr.count==2) {
            VideoModel *vmodel=[arr objectAtIndex:1];
            cell.lblDetail1.text=vmodel.category_NAME;
            cell.lblName1.text=vmodel.video_NAME;
            cell.btn2.tag=indexPath.section*10+1;
            [cell.btn2 addTarget:self action:@selector(playVideoAction:) forControlEvents:UIControlEventTouchUpInside];
        }

    }

    
    
    return cell;
}

-(void)moreVideoAction:(id)sender{
    UIButton *btn=(UIButton*)sender;
    NSLog(@"%ld",(long)btn.tag);
    DictData *dd =[sectionArr objectAtIndex:btn.tag];
    VideoListViewController *vc=[[VideoListViewController alloc]init];
    [vc setCATEGORY:dd.name];
    [vc setCLASSIFICATION:dd.zd_ID];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)playVideoAction:(id)sender{
    UIButton *btn=(UIButton*)sender;
    NSLog(@"%ld",(long)btn.tag);
    MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:@"http://riyu.oss-cn-shanghai.aliyuncs.com/riyu/BDB9D790-2266-48A4-824F-5063E4223FD6.MOV"]];
    [self presentMoviePlayerViewControllerAnimated:playerViewController];
}



- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
